/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package TestProject;

/**
 *
 * @author Administrator
 */
import DlinkedList.DNode;
import DlinkedList.MyDList;
import java.io.*;
import message.*;
import folders2.*;
import GoMail.*;
import account.Account;
                                                //System.out.println();
public class ProjectTest
{
    public static String readUserInput()throws IOException
    {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        return in.readLine();
    }

    public static void options(MailUserOperations userMail,UserFileOperation2 users, RootFolders2 root)throws Exception
    {
        while(true)
        {
            System.out.println("To operate on messages enter 0");
            System.out.println("To operate on folders enter 1");
            System.out.println("To operate on contacts enter 2");
            System.out.println("Enter 3 to exist");
            int num = Integer.parseInt(readUserInput());

            switch(num)
            {
                case 0:
                {
                    messageOperation(userMail, root.getTemp().getMyFile(), users);
                };break;
                case 1:
                {
                    folderOperation(userMail);
                };break;
                case 2:
                {
                    contactsOperation(userMail);
                };break;
                case 3:
                {
                    userMail.writeContactFile();
                    users.writeUsersFile();
                    userMail.writeFilterFile();
                    System.exit(0);
                };break;
            }
        }
    }

    public static void messageOperation(MailUserOperations userMail, File temp, UserFileOperation2 users) throws Exception
    {
        System.out.println("to see your message in any folder enter 0");
        System.out.println("to filtr messages enter 1");
        System.out.println("to send message enter 2");
        int input = Integer.parseInt(readUserInput());
        switch(input)
        {
            case 0:
            {
                System.out.println("Enter Folder name");
                String name = readUserInput();
                MessageFolder[] allSms = userMail.loadAllMailsFormFolder(name);      //all messages without any order
                userMail.loadMessagesIntoList(allSms, 0);           //to read first recent messages and load them into list
                DNode u = userMail.getMessage().getHeader();
                for(int i =0; i< userMail.getMessage().getSize(); i++)
                {
                    u = u.getNext();
                    System.out.println(((MessageFolder)u.getData()).getSms().getSubject());
                }
                boolean continuous = true;
                while(continuous)
                {
                    System.out.println("to operate on specific message enter 0");
                    System.out.println("to read next ten messages enter 1  ");
                    System.out.println("to read previous ten messages enter 2");
                    System.out.println("To more options on messages enter 3");
                    System.out.println("To turn to another thing enter 4");
                    switch(Integer.parseInt(readUserInput()))
                    {
                        case 0:
                        {
                            System.out.println("Enter message name to operate on it");
                            MessageFolder requestedFolder = userMail.getMessageBySubject(userMail.getMessage(), readUserInput());
                            operateOnSpecificMessage(userMail, requestedFolder);
                        };break;
                        case 1:
                        {
                            userMail.loadMessagesIntoList(allSms, 1);
                            displayMessagesSubject(userMail.getMessage());
                        };break;
                        case 2:
                        {
                            userMail.loadMessagesIntoList(allSms, 2);
                            displayMessagesSubject(userMail.getMessage());
                        };break;
                        case 3:
                        {
                            messageOptions(userMail, allSms);
                        }
                        case 4:
                        {
                            continuous = false;
                        };break;
                    }
                }

            };break;
            case 1:
            {
                 System.out.println("To filter by subject enter 0");
                System.out.println("To filter by sender enter 1");
                input = Integer.parseInt(readUserInput());
                switch(input)
                {
                    case 0:
                    {
                        Filter f = new Filter();
                        System.out.println("Enter message subject");
                        f.setSubject(readUserInput());
                        System.out.println("enter direction folder name");
                        f.setDirectionFolder(readUserInput());
                        userMail.getFilterList().add(f);
                    };break;
                    case 1:             //filter by sender
                    {
                        Filter f = new Filter();
                        System.out.println("enter message sender");
                        f.setSubject(readUserInput());
                        System.out.println("enter direction folder name");
                        f.setDirectionFolder(readUserInput());
                        userMail.getFilterList().add(f);
                    };break;
                }
            };break;
            case 2:                         //send message
            {
                boolean continuous = true;
                while(continuous)
                {
                    System.out.println("To send message enter 0");
                    System.out.println("To turn to another thing enter 1");
                    switch(Integer.parseInt(readUserInput()))
                    {
                        case 0:
                        {
                            System.out.println("Enter message text");
                            String text = readUserInput();
                            Attachment attach = new Attachment();
                            do
                            {
                                System.out.println("if you want to add attachment enter 1 not enter 0");
                                input = Integer.parseInt(readUserInput());
                                if(input != 0)
                                {
                                    System.out.println("Enter attach path");
                                    attach.addPath(readUserInput());
                                }
                            }while(input != 0);
                            System.out.println("Enter message details in the following form");
                            System.out.println("Subject");
                            String messageSubject = readUserInput();
                            System.out.println("receivers mails receive1,receive2,......");
                            String[] receivers = readUserInput().split(",");
                            Message[] sms = userMail.makeMultipleMessages(messageSubject, receivers);
                            System.out.println("To confirm sending message enter 0 else enter 1");

                                if(Integer.parseInt(readUserInput()) == 0)
                                {
                                    userMail.sendMultipleMessages(temp, text, attach, sms);
                                    userMail.sendMultipleMessages(userMail.getUser().getFolders().getSent().getMyFile(), text, attach, sms);     //make acopy of message in sent folder
                                    for(int i=0;i<receivers.length;i++)
                                    {
                                        users.incrementMessageCounter(receivers[i]);        ///////increment number of messages in mail server users file
                                    }
                                }
                                else
                                {
                                    userMail.sendMultipleMessages(userMail.getUser().getFolders().getDraft().getMyFile(), text, attach, sms);
                                }

                        };break;
                        case 1:
                        {
                            continuous = false;
                        };break;
                    }
                }
            };break;
        }
    }
    public static void displayMessagesSubject(MyDList messages)
    {
        DNode u = messages.getHeader();
        for(int i =0; i< messages.getSize(); i++)
        {
            u = u.getNext();
            System.out.println(((MessageFolder)u.getData()).getSms().getSubject());
        }
    }
    public static void messageOptions(MailUserOperations userMail,MessageFolder[] allSms) throws Exception
    {
        System.out.println("Sorting messages enter 0");
        System.out.println("Searching in messages enter 1");
        switch(Integer.parseInt(readUserInput()))
        {
            case 0:
            {
                sortingOptions(userMail,allSms);
            };break;
            case 1:
            {
                searchingOptions(userMail, allSms);
            };break;
        }
    }
                                                                    //allSms in specific folder
    public static void sortingOptions(MailUserOperations userMail,MessageFolder[] allSms) throws Exception
    {
        System.out.println("Sorting by date enter 0");
        System.out.println("Sorting by sender enter 1");
        System.out.println("Sorting by subject enter 2");
        System.out.println("Sorting by atttachment enter 3");
        switch(Integer.parseInt(readUserInput()))
        {
            case 0:
            {
                System.out.println("Messages already sorted by date");
            };break;
            case 1:
            {
                MessageFolder[] sortedList = userMail.sortingBySender(allSms);
                userMail.loadMessagesIntoList(sortedList, 0);
                displayMessagesSubject(userMail.getMessage());
                System.out.println("to read next ten messages enter 0  ");
                System.out.println("to read previous ten messages enter 1");
                switch(Integer.parseInt(readUserInput()))
                {
                    case 0:
                    {
                        userMail.loadMessagesIntoList(sortedList, 1);
                        displayMessagesSubject(userMail.getMessage());
                    };break;
                    case 1:
                    {
                        userMail.loadMessagesIntoList(sortedList, 2);
                        displayMessagesSubject(userMail.getMessage());
                    };break;
                }
            };break;
            case 2:
            {
                MessageFolder[] sortedList = userMail.sortingBySubject(allSms);
                userMail.loadMessagesIntoList(sortedList, 0);
                displayMessagesSubject(userMail.getMessage());
                System.out.println("to read next ten messages enter 0  ");
                System.out.println("to read previous ten messages enter 1");
                switch(Integer.parseInt(readUserInput()))
                {
                    case 0:
                    {
                        userMail.loadMessagesIntoList(sortedList, 1);
                        displayMessagesSubject(userMail.getMessage());
                    };break;
                    case 1:
                    {
                        userMail.loadMessagesIntoList(sortedList, 2);
                        displayMessagesSubject(userMail.getMessage());
                    };break;
                }
            };break;
            case 3:
            {
                MessageFolder[] sortedList = userMail.sortingByAttachment2(allSms);
                userMail.loadMessagesIntoList(sortedList, 0);
                displayMessagesSubject(userMail.getMessage());
                System.out.println("to read next ten messages enter 0  ");
                System.out.println("to read previous ten messages enter 1");
                switch(Integer.parseInt(readUserInput()))
                {
                    case 0:
                    {
                        userMail.loadMessagesIntoList(sortedList, 1);
                        displayMessagesSubject(userMail.getMessage());
                    };break;
                    case 1:
                    {
                        userMail.loadMessagesIntoList(sortedList, 2);
                        displayMessagesSubject(userMail.getMessage());
                    };break;
                }
            };break;
        }
    }

    public static void searchingOptions(MailUserOperations userMail,MessageFolder[] allSms) throws Exception
    {
        System.out.println("Searching by date enter 0");
        System.out.println("Searching by sender enter 1");
        System.out.println("Searching by subject enter 2");
        /*System.out.println("Searching by atttachment enter 3");//?????????????*/
        switch(Integer.parseInt(readUserInput()))
        {
            case 0:
            {
                System.out.println("enter date in this form yyyy-dd-mm");
                String date = readUserInput();
                userMail.searchingByDate(allSms, date);
                displayMessagesSubject(userMail.getMessage());
            };break;
            case 1:
            {
                System.out.println("enter sender mail");
                String senderMail = readUserInput();
                userMail.searchingBySender(allSms, senderMail);
                displayMessagesSubject(userMail.getMessage());
            };break;
            case 2:
            {
                System.out.println("enter message subject");
                String date = readUserInput();
                userMail.searchingBySubject(allSms, date);
                displayMessagesSubject(userMail.getMessage());
            };break;
            /*case 3:
            {
            System.out.println("enter ");
            String date = readUserInput();
            userMail.searchingBy........(allSms, ............);
            displayMessagesSubject(userMail.getMessage());
            };break;*/
        }
    }

    public static void operateOnSpecificMessage(MailUserOperations userMail,MessageFolder requestedFolder) throws IOException
    {
        boolean continuous = true;
        while(continuous)
        {
            System.out.println("to read  message enter 0");
            System.out.println("to delete  message enter 1");
            System.out.println("to filter  message enter 2");
            System.out.println("to download message attachment enter 3");
            System.out.println("to turn to another thing enter 4");
            int input = Integer.parseInt(readUserInput());
            switch(input)
            {
                case 0:
                {
                    System.out.println("message text "+requestedFolder.readTextFile());
                    System.out.println("message sender"+requestedFolder.getSms().getSender());
                    System.out.println("message send date"+requestedFolder.getSms().getSendDate());
                    requestedFolder.getSms().setRead(true);
                    requestedFolder.writeMessageInfo();
                };break;
                case 1:
                {

                    userMail.deleteMessage(requestedFolder);
                    continuous = false;
                };break;
                case 2:
                {
                    continuous = false;
                    System.out.println("To filter by subject enter 0");
                    System.out.println("To filter by sender enter 1");
                    input = Integer.parseInt(readUserInput());
                    switch(input)
                    {
                        case 0:
                        {
                            Filter f = new Filter();
                            f.setSubject(requestedFolder.getSms().getSubject());
                            System.out.println("enter direction folder name");
                            f.setDirectionFolder(readUserInput());
                            userMail.getFilterList().add(f);
                            userMail.filterBySubject(requestedFolder);
                        };break;
                        case 1:             //filter by sender
                        {
                            Filter f = new Filter();
                            f.setSubject(requestedFolder.getSms().getSender());
                            System.out.println("enter direction folder name");
                            f.setDirectionFolder(readUserInput());
                            userMail.getFilterList().add(f);
                            userMail.filterBySender(requestedFolder);
                        };break;
                    }
                };break;
                case 3:
                {
                    System.out.println("Enter path to download message attachment");
                    userMail.downloadAttachment(readUserInput(), requestedFolder.getAttachFolder().getMyFile());
                };break;
                case 4:
                {
                    continuous = false;
                }
            }
        }

    }
    public static void folderOperation(MailUserOperations userMail) throws IOException
    {
        boolean continuous = true;
        while(continuous)
        {
            System.out.println("Enter 0 to rename folder");
            System.out.println("Enter 1 to delete folder");
            System.out.println("Enter 2 to create new folder");
            System.out.println("Enter 3 to turn to another thing");
            int v = Integer.parseInt(readUserInput());
            switch(v)
            {
                case 0:
                {
                    System.out.println("Enter folder old name");
                    String oldName = readUserInput();
                    System.out.println("Enter folder new name");
                    String newName = readUserInput();
                    userMail.renameFolder(oldName, newName);
                };break;
                case 1:
                {
                    System.out.println("Enter folder name");
                    userMail.deleteFolder(readUserInput());
                };break;
                case 2:
                {
                    System.out.println("Enter folder name");
                    userMail.getUser().getFolders().createCustomFolder(readUserInput());
                };break;
                case 3:
                {
                    continuous = false;
                }
            }
        }

    }

    public static void contactsOperation(MailUserOperations userMail) throws IOException
    {
        System.out.println("Add new contact enter 0");
        System.out.println("get contact enter 1");
        System.out.println("To delete contact enter 2");
        switch(Integer.parseInt(readUserInput()))
        {
            case 0:
            {
                System.out.println("enter contact name && mails mail(1),mail(2),.....");
                String[]s = readUserInput().split(",");
                Contacts c = new Contacts();
                c.setContactData(s);
                userMail.addContact(c);
            };break;
            case 1:
            {
                System.out.println("enter contact name");
                Contacts u = userMail.findContact(readUserInput());
                if(u != null)
                {
                  System.out.println("contact name "+u.getName());
                  displayInfo(u.allMails());
                  System.out.println("To edit in contact data enter 0 otherwise enter 1");
                  int number = Integer.parseInt(readUserInput());
                  if(number == 0)
                  {
                      System.out.println("To edit in contact name enter 0");
                      System.out.println("To edit in contact emails 1");
                      switch(Integer.parseInt(readUserInput()))
                      {
                          case 0:
                          {
                              System.out.println("Enter contact new name");
                              String newName = readUserInput();
                              u.setName(newName);
                          };break;
                          case 1:
                          {
                              System.out.println("Enter contact mails in this form mail1,mail2,........");
                              String[] contactMails = readUserInput().split(",");
                              u.setContactMails(contactMails);
                          };break;
                      }
                  }
                }
            };break;
            case 2:
            {
                System.out.println("Enter contact name");
                String contactName = readUserInput();
                userMail.deleteContact(contactName);
            };break;
        }
    }

    public static void displayInfo(String[]s)
    {
        for(int i=0; i < s.length; i++)
            System.out.println(s[i]);
    }

    public static void main(String[]args)throws Exception
    {
        UserFileOperation2 users = new UserFileOperation2();
        users.readUsersFile();
        //i should make chech in this step of root folders
        RootFolders2 rootFolders = new RootFolders2();
        if(!rootFolders.getRoot().getMyFile().exists())
            rootFolders.createRootFolders();
        MailUserOperations newUser = null;
        while(true)
        {
            System.out.println(" 1-To sign up enter 0 \n 2-To sign in enter 1\n 3-To exit enter 2");
            int value = Integer.parseInt(readUserInput());
            String[]s;
            switch(value)
            {
                case 0:
                {
                       System.out.println("Enter details as follow firstName,lastName,userName,password"+
                        ",lternativeMail\n,city,country,formatter.format(birthday),"
                        +"gender,securityQn,qnAnswer,postalCode");
                        s = readUserInput().split(",");
                        Account userAccount = new Account();
                        userAccount.setAllAccountData(s);
                        UsersFile2 userData = new UsersFile2(userAccount.getUserName(),userAccount.getPassword());
                        users.addNewUser(userData);         //add new user to users file
                        /*users.getUsers().add(new UsersFile2(userAccount.getUserName(),userAccount.getPassword()));*/
                        MailUser userMail = new MailUser(rootFolders.getRoot().getMyFile(),userAccount);//////////////?????????????
                        userMail.getUserAccount().writeAccountData(userMail.getFolders().getMyDir().getMyFile());
                        newUser = new MailUserOperations(rootFolders.getRoot().getMyFile(), userAccount);
                        newUser.setUser(userMail);
                        options(newUser, users, rootFolders);
                };break;
                case 1:
                {
                     System.out.println("Enter in this form userName,password");
                     s = readUserInput().split(",");
                     int sentMessageCount = users.mailExist(s[0], s[1]);
                     if(sentMessageCount != -1)
                     {
                         newUser = new MailUserOperations();
                         newUser.findUser(s[0], rootFolders.getRoot());
                         if(sentMessageCount != 0)
                         {
                            newUser.moveMessageToUserFolder(rootFolders.getTemp(), sentMessageCount);
                            users.decrementMessageCounter(newUser.getUser().getUserAccount().getUserName());
                         }
                         System.out.println("you have "+sentMessageCount+" new messages");
                         options(newUser, users, rootFolders);
                     }
                     else
                         System.out.println("not found");
                };break;
                case 2:
                {
                    users.writeUsersFile();
                    if(newUser != null)
                    {
                        newUser.writeContactFile();
                        newUser.writeFilterFile();
                    }
                    System.exit(0);
                };break;
            }
        }
    }
}
